package com.ky.tms.controller;


import com.ky.tms.entity.TmsOrder;
import com.ky.tms.exception.PredictableException;
import com.ky.tms.service.ITmsOrderService;
import com.ky.tms.util.CommonResult;
import com.ky.tms.util.JwtUtil;
import com.ky.tms.vo.EntryOrder;
import com.ky.tms.vo.OrderIncome;
import com.ky.tms.vo.TrackOrder;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chengkaimao
 * @since 2020-03-24
 */
@RestController
@RequestMapping("/tms/tms-order")
public class TmsOrderController {

    @Autowired
    private ITmsOrderService tmsOrderService;


    @ApiOperation("输单分页数据")
    @GetMapping(value = "entry-page")
    public CommonResult entryPage(TmsOrder tmsOrder, @RequestParam Long pageSize, @RequestParam Long current, HttpServletRequest request){
        String userName = JwtUtil.getUserName(request);
        return CommonResult.buildSuccess(tmsOrderService.entryPage(userName,tmsOrder,pageSize, current));
    }

    @ApiOperation("实际费用分页数据")
    @GetMapping(value = "income-page")
    public CommonResult incomePage(TmsOrder tmsOrder, @RequestParam Long pageSize, @RequestParam Long current, HttpServletRequest request){
        String userName = JwtUtil.getUserName(request);
        return CommonResult.buildSuccess(tmsOrderService.incomePage(userName,tmsOrder,pageSize,current));
    }

    @ApiOperation("开票分页数据")
    @GetMapping(value = "invoice-page")
    public CommonResult invoicePage(TmsOrder tmsOrder, @RequestParam Long pageSize, @RequestParam Long current, HttpServletRequest request){
        String userName = JwtUtil.getUserName(request);
        return CommonResult.buildSuccess(tmsOrderService.invoicePage(userName,tmsOrder,pageSize,current));
    }
    @ApiOperation("收入审核分页数据")
    @GetMapping(value = "income-audit-page")
    public CommonResult incomeAuditPage(TmsOrder tmsOrder, @RequestParam Long pageSize, @RequestParam Long current, HttpServletRequest request){
        String userName = JwtUtil.getUserName(request);
        return CommonResult.buildSuccess(tmsOrderService.incomeAuditPage(userName,tmsOrder,pageSize,current));
    }

    @ApiOperation("新增输单")
    @PostMapping("entry")
    public CommonResult entryAdd(@RequestBody EntryOrder entryOrder, HttpServletRequest request) throws PredictableException{
        String userName = JwtUtil.getUserName(request);
        tmsOrderService.entryAdd(userName, entryOrder);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("修改输单")
    @PutMapping("entry")
    public CommonResult entryUpdate(@RequestBody EntryOrder entryOrder, HttpServletRequest request) throws PredictableException {
        String userName = JwtUtil.getUserName(request);
        tmsOrderService.entryUpdate(userName, entryOrder);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("删除输单")
    @DeleteMapping("entry")
    public CommonResult entryDel(@RequestBody List<Long> ids, HttpServletRequest request){
        String userName = JwtUtil.getUserName(request);
        return CommonResult.buildSuccess(tmsOrderService.entryDel(userName,ids));
    }

    @ApiOperation("输单提交")
    @PostMapping("entry-submit")
    public CommonResult entrySubmit(@RequestBody List<Long> ids, HttpServletRequest request) {
        String userName = JwtUtil.getUserName(request);
        return CommonResult.buildSuccess(tmsOrderService.entrySubmit(userName,ids));
    }

    @ApiOperation("拆单分页数据")
    @GetMapping("split-page")
    public CommonResult splitPage(TmsOrder tmsOrder, @RequestParam Long pageSize, @RequestParam Long current, HttpServletRequest request){
        String userName = JwtUtil.getUserName(request);
        return CommonResult.buildSuccess(tmsOrderService.splitPage(userName,tmsOrder,pageSize, current));
    }

    @ApiOperation("拆单保存")
    @PostMapping("split")
    public CommonResult splitSave(@RequestBody TmsOrder tmsOrder, HttpServletRequest request) throws PredictableException {
        String userName = JwtUtil.getUserName(request);
        tmsOrderService.splitSave(userName, tmsOrder);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("拆单删除")
    @DeleteMapping("split/{id}")
    public CommonResult splitDel(@PathVariable Long id){
        return CommonResult.buildSuccess(tmsOrderService.splitDel(id));
    }

    @ApiOperation("拆单删除(load号)")
    @DeleteMapping("split-ln/{loadNo}")
    public CommonResult splitDelLn(@PathVariable String loadNo){
        return CommonResult.buildSuccess(tmsOrderService.splitDelLn(loadNo));
    }


    @ApiOperation("调度分页数据")
    @GetMapping("dispatch-page")
    public CommonResult dispatchPage(TmsOrder tmsOrder, @RequestParam Long pageSize, @RequestParam Long current, HttpServletRequest request){
        String userName = JwtUtil.getUserName(request);
        return CommonResult.buildSuccess(tmsOrderService.dispatchPage(userName,tmsOrder,pageSize, current));
    }

    @ApiOperation("撤消拆单")
    @PostMapping("rollback-split")
    public CommonResult dispatchPage(@RequestBody TmsOrder tmsOrder, HttpServletRequest request) throws PredictableException {
        String userName = JwtUtil.getUserName(request);
        tmsOrderService.rollbackSplit(userName, tmsOrder);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("订单审核分页数据")
    @GetMapping("audit-page")
    public CommonResult auditPage(TmsOrder tmsOrder, @RequestParam Long pageSize, @RequestParam Long current, HttpServletRequest request){
        String userName = JwtUtil.getUserName(request);
        return CommonResult.buildSuccess(tmsOrderService.auditPage(userName,tmsOrder,pageSize, current));
    }


    @ApiOperation("订单审核")
    @PostMapping("audit")
    public CommonResult audit(@RequestBody TmsOrder tmsOrder, HttpServletRequest request) throws PredictableException {
        String userName = JwtUtil.getUserName(request);
        tmsOrderService.auditOrder(userName, tmsOrder);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("拆单驳回")
    @PostMapping("split-reject")
    public CommonResult splitReject(@RequestBody TmsOrder tmsOrder, HttpServletRequest request) throws PredictableException {
        String userName = JwtUtil.getUserName(request);
        tmsOrderService.splitReject(userName,tmsOrder);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("填写实际收入")
    @PostMapping("income")
    public CommonResult income(@RequestBody TmsOrder tmsOrder, HttpServletRequest request) throws PredictableException {
        String userName = JwtUtil.getUserName(request);
        tmsOrderService.income(userName,tmsOrder);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("保存实际收入")
    @PostMapping("income-save")
    public CommonResult incomeSave(@RequestBody OrderIncome orderIncome, HttpServletRequest request) throws PredictableException {
        String userName = JwtUtil.getUserName(request);
        tmsOrderService.incomeSave(userName, orderIncome);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("开票")
    @PostMapping("invoice")
    public CommonResult invoice(@RequestBody List<Long> ids, HttpServletRequest request) throws PredictableException {
        String userName = JwtUtil.getUserName(request);
        tmsOrderService.invoice(userName,ids);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("收款")
    @PostMapping("pay")
    public CommonResult pay(@RequestBody List<Long> ids, HttpServletRequest request) throws PredictableException {
        String userName = JwtUtil.getUserName(request);
        tmsOrderService.pay(userName,ids);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("追踪订单分页数据")
    @GetMapping("track-page")
    public CommonResult trackPage(TrackOrder trackOrder, @RequestParam Long pageSize, @RequestParam Long current, HttpServletRequest request){
        String userName = JwtUtil.getUserName(request);
        return CommonResult.buildSuccess(tmsOrderService.trackPage(userName,trackOrder,pageSize, current));
    }

    @ApiOperation("追踪订单发送")
    @PostMapping("track-pass")
    public CommonResult trackPass(@RequestBody TrackOrder trackOrder, HttpServletRequest request) throws PredictableException {
        String userName = JwtUtil.getUserName(request);
        tmsOrderService.trackPass(userName, trackOrder);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("追踪订单驳回")
    @PostMapping("track-reject")
    public CommonResult trackReject(@RequestBody TrackOrder trackOrder, HttpServletRequest request) throws PredictableException {
        String userName = JwtUtil.getUserName(request);
        tmsOrderService.trackReject(userName, trackOrder);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("实际费用提交")
    @PostMapping("income-submit")
    public CommonResult incomeSubmit(@RequestBody List<Long> ids, HttpServletRequest request) throws PredictableException {
        String userName = JwtUtil.getUserName(request);
        tmsOrderService.incomeSubmit(userName, ids);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("实际费用审核")
    @PostMapping("income-audit")
    public CommonResult inComeAudit(@RequestBody List<Long> ids, HttpServletRequest request) throws PredictableException {
        String userName = JwtUtil.getUserName(request);
        tmsOrderService.incomeAudit(userName, ids);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("实际收入驳回")
    @PostMapping("income-reject")
    public CommonResult inComeReject(@RequestBody List<Long> ids, HttpServletRequest request) throws PredictableException {
        String userName = JwtUtil.getUserName(request);
        tmsOrderService.incomeReject(userName, ids);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("导入输单")
    @PostMapping(value = "/reportEntry")
    public CommonResult reportEntry(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws PredictableException {
        String userName = JwtUtil.getUserName(request);
        tmsOrderService.reportEntry(userName, file);
        return CommonResult.buildSuccess(null);
    }

    @ApiOperation("开票驳回")
    @PostMapping(value = "invoice-reject")
    public CommonResult invoiceReject(@RequestBody Long id) throws PredictableException {
        tmsOrderService.invoiceReject(id);
        return CommonResult.buildSuccess(null);
    }

}
